package cn.nexd.app.collector.widget.sliding;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import cn.nexd.app.collector.util.ScreenUtils;

/**
 * Created by xun on 2016/8/29.
 */
public class SlidingBlock extends ImageButton {
    private int marginLeftOffset;
    private int eventX;
    private OnSlidingListener slidingListener;
    private int width;
    private int height;
    private int mParentWidth;
    private boolean isStart = false;
    private int stopFinalX;
    private static final String TAG = "SlidingBlock";
    private boolean isMeasure = false;
    private boolean isReset;

    public void setSlidingListener(OnSlidingListener slidingListener) {
        this.slidingListener = slidingListener;
    }

    public SlidingBlock(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (!isMeasure) {
            width = MeasureSpec.getSize(widthMeasureSpec);
            height = MeasureSpec.getSize(heightMeasureSpec);
            setMeasuredDimension(width, height);
            isMeasure = true;
        }

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                eventX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                handleMoveEvent(event);
                break;
            case MotionEvent.ACTION_UP:
                handleUpEvent();

                break;
        }
        return super.onTouchEvent(event);
    }

    private void handleUpEvent() {
        mParentWidth = ((ViewGroup) this.getParent()).getWidth();
        //到达最右端，开启成功
        if (this.getLeft() + getWidth() == mParentWidth) {
            if (slidingListener != null) {
                slidingListener.onRight();
                if (isReset) {
                    isStart = false;
                    isReset = false;
                } else {
                    isStart = true;
                }
            }
        } else {
            if (isStart) {
                //如果已经开启成功
                if (this.getLeft() + getWidth() < mParentWidth / 4.5) {
                    if (slidingListener != null) {
                        slidingListener.onLeft();
                        isStart = false;
                    }
                } else {
                    stopAnimation(500);
                }


            } else {
                startAnimation(200);
            }
        }

    }

    public void reset() {
        if (slidingListener != null) {
            slidingListener.onLeft();
            isStart = false;
            isReset = true;
            startAnimation(200);
        }
    }

    private void startAnimation(int time) {
        TranslateAnimation trans =
                new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -this.getLeft(),
                        Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
        trans.setDuration(time);
        trans.setInterpolator(new AccelerateInterpolator());
        trans.setAnimationListener(new SlidingAnimationListener(this));
        startAnimation(trans);
    }

    private void stopAnimation(int time) {
        stopFinalX = mParentWidth - getWidth();
        TranslateAnimation trans =
                new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, stopFinalX - getLeft(),
                        Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);

        trans.setDuration(time);
        trans.setFillAfter(true);
        trans.setInterpolator(new AccelerateInterpolator());
        trans.setAnimationListener(new SlidingStopAnimationListener(this));
        startAnimation(trans);
    }

    private class SlidingStopAnimationListener implements Animation.AnimationListener {

        private SlidingBlock btn;

        public SlidingStopAnimationListener(SlidingBlock button) {
            this.btn = button;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            rePosition();
            btn.clearAnimation();
        }

        private void rePosition() {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) btn.getLayoutParams();
            lp.leftMargin = stopFinalX;
            btn.setLayoutParams(lp);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationStart(Animation animation) {

        }

    }

    private class SlidingAnimationListener implements Animation.AnimationListener {

        private SlidingBlock btn;

        public SlidingAnimationListener(SlidingBlock button) {
            this.btn = button;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            rePosition();
            btn.clearAnimation();
        }

        private void rePosition() {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) btn.getLayoutParams();
            lp.leftMargin = 0;
            btn.setLayoutParams(lp);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationStart(Animation animation) {

        }

    }

    private void handleMoveEvent(MotionEvent event) {
        mParentWidth = ((ViewGroup) this.getParent()).getWidth();
        int x = (int) event.getX();
        int marginLeft = getLeft() + (x - eventX);
        if (!isStart) {
            if (marginLeft < marginLeftOffset) {
                marginLeft = marginLeftOffset;
            } else if (marginLeft > mParentWidth - width + ScreenUtils.px2dp(getContext(), 16)) {
                marginLeft = mParentWidth - width + ScreenUtils.px2dp(getContext(), 16);
                Log.d(TAG, "handleMoveEvent:  max" + marginLeft);
            }
        } else {
            if (marginLeft > mParentWidth - width + ScreenUtils.px2dp(getContext(), 16)) {
                marginLeft = mParentWidth - width + ScreenUtils.px2dp(getContext(), 16);
            }

        }


        if (marginLeft < 0) {
            marginLeft = 0;
        }

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
        lp.leftMargin = marginLeft;
        Log.d(TAG, "handleMoveEvent: left  width" + marginLeft + "..........." + width);
        setLayoutParams(lp);
    }



}
